const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const router = require('./router/index')
// 错误处理中间件
const errorHandler = require('./middleware/error-handler')
// 数据库
require('./model')

const app = express()

// 配置日志输出中间件
app.use(morgan('dev'))

// 配置解析请求体
app.use(express.json())
app.use(express.urlencoded())

// 配置跨域
app.use(cors())

// 如果环境变量里有，就默认读取环境变量里的
const PORT = process.env.PORT || 3000

// 挂载路由
app.use('/api', router)

// 挂载统一处理服务端错误中间件
app.use(errorHandler())

app.listen(PORT, () => {
    console.log(`Server is running at http://localhost:${PORT}`);
})